/**
 * Created by yihua on 2017/3/1.
 */
$(document).ready(function () {
    $('#editflashsaleModal').on('shown.bs.modal', function (e) {
        $('#edit_start_time').datetimepicker({format: 'yyyy-mm-dd hh:ii', autoclose: true, todayBtn: true});
        $('#edit_end_time').datetimepicker({format: 'yyyy-mm-dd hh:ii', autoclose: true, todayBtn: true});
    });

    $(".editflashsale").each(function () {
        var that = $(this);
        that.click(function () {
            console.log($(this))
            var id = that.parent().find("td").eq(0).text()
            var objTd = that.parent().find("td");
            initGoodsInfo();
            //fillInput
            $("#edit_id").val(objTd.eq(0).text());
            $("#edit_title").val(objTd.eq(1).text());
            $("#edit_description").val(objTd.eq(2).text());
            $("#edit_price").val(objTd.eq(5).text());
            $("#edit_goods_num").val(objTd.eq(6).text());
            $("#edit_buy_limit").val(objTd.eq(7).text());
            $("#edit_start_time").val(objTd.eq(11).text());
            $("#edit_end_time").val(objTd.eq(12).text());
            $("#edit_goods_id").val(objTd.eq(3).text()).trigger("change");

            $("#editflashsaleModal").modal("show");

            $("#edit_confirm").unbind('click').click(function () {
                var options = {
                    url: '/home/shop/updateflashsale',
                    beforeSubmit: validate,  // pre-submit callback
                    success: showResponse,  // post-submit callback
                    dataType: 'json',
                    type: 'post',
                    data: {
                        'goods_name': $("#edit_goods_id option:selected").text(),
                        'id': id,
                    }
                };
                $("#editflashsaleForm").ajaxSubmit(options);
            });
        });
    });


    function validate(formData, jqForm, options) {
        for (var i = 0; i < formData.length; i++) {
            if (formData[i].name == 'start_time' || formData[i].name == 'end_time') {
                formData[i].value = Date.parse(new Date(formData[i].value)) / 1000;
            }

            if (!formData[i].value) {
                console.log(formData[i].name)
                toastr.error('请输入所有选项', '', {positionClass: 'toast-bottom-center'});
                return false;
            }
        }
    }

    function showResponse(responseText, statusText, xhr, $form) {
        console.log(responseText);
        if (0 !== responseText.errno) {
            toastr.error(responseText.errmsg, '', {positionClass: 'toast-bottom-center'});
        } else {
            toastr.success('成功', '', {positionClass: 'toast-bottom-center'});
            $("#editflashsaleModal").modal("hide");
            window.location.reload();
        }
    }


    function initGoodsInfo() {
        $.ajax({
            url: '/home/shop/goodslist',
            timeout: 100000,
            type: 'get',
            dataType: 'json',
            async: false,
            data: {
                'type': 'list',
            },
            success: function (rs) {
                //console.log(rs)
                if (rs.errno != 0) {
                    toastr.error(rs.errno);
                    return false;
                } else {
                    var myData = [];
                    for (var i = 0; i < rs.data.length; i++) {
                        myData.push({'id': rs.data[i].goods_id, 'text': rs.data[i].goods_name})
                    }
                    //console.log(myData)
                    $("#edit_goods_id").select2({
                        dropdownParent: $("#editflashsaleModal"),
                        data: myData,
                    })
                    //$("#edit_goods_id").val($(this).parent().find("td").eq(3).text()).trigger("change");
                }
            },
            error: function () {
                toastr.error('系统错误');
            }
        });
    }
});